﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;

namespace AirQuality
{
    public class MainViewModel : ViewModelBase
    {
        private ObservableCollection<SensorMessage> _SensorMessage;
        public ObservableCollection<SensorMessage> SensorMessage
        {
            set
            {
                _SensorMessage = value;
                //Console.WriteLine("sensorMessage data is {0}",_SensorMessage[0].Data);
                RaisePropertyChanged("SensorMessage");
            }
            get
            {
                return _SensorMessage;
            }
        }

        private Visibility mess;
        public Visibility Mess
        {
            set
            {
                mess = value;
                RaisePropertyChanged<Visibility>("Mess");
            }
            get
            {
                return mess;
            }
        }

        public RelayCommand ExitCommand { get; set; }

        public RelayCommand SettingCommand { get; set; }

        public DispatcherTimer Timer { get; set; }

        private MainWindow MainWindow { get; set; }

        public MainViewModel(MainWindow mainWindow)
        {
            MainWindow = mainWindow;
            _SensorMessage = new ObservableCollection<SensorMessage>()
            {
                new SensorMessage()
                {
                    Data = 0,
                    Name = "PM1.0",
                    Unit = "ug/m^3",
                    Foregroud = Brushes.Orange
                },
                new SensorMessage()
                {
                    Name = "PM2.5",
                    Unit = "ug/m^3",
                    Foregroud = Brushes.OrangeRed,
                    Tooltip = "标准范围为小于75"
                },
                new SensorMessage()
                {
                    Name = "PM10",
                    Unit = "ug/m^3",
                    Foregroud = Brushes.Red,
                    Tooltip = "标准范围为小于150"
                }
            };
            ExitCommand = new RelayCommand(Exit);
            SettingCommand = new RelayCommand(Setting);
            //注册消息事件刷新数据
            Messenger.Default.Register<bool>(this, "DataUpdate", DataUpdate);

            Messenger.Default.Register<bool>(this, "SerialERR", SerialERR);
        }

        private void SerialERR(bool obj)
        {
            Thread.Sleep(100);
            Mess = Server.serialPort.IsOpen ? Visibility.Hidden : Visibility.Visible;
        }

        private void Exit()
        {
            Server.ConfigJson["x"] = MainWindow.Left;
            Server.ConfigJson["y"] = MainWindow.Top;
            Server.SaveConfigToFile();
            Application.Current.MainWindow.Close();
        }

        private void Setting()
        {
            Server.IsSetting = true;
            SettingWindows settingWindows = new SettingWindows
            {
                Owner = MainWindow
            };
            if (Server.serialPort.IsOpen)
            {
                Server.serialPort.Close();
            }
            while (true)
            {
                bool? r = settingWindows.ShowDialog();
                if (r.HasValue && r.Value)//parameter has changed
                {
                    try
                    {
                        Server.serialPort.Open();
                        Messenger.Default.Send(true, "SerialERR");
                        Console.WriteLine(Server.serialPort.PortName);
                    }
                    catch(System.IO.IOException)
                    {
                        Messenger.Default.Send(false, "SerialERR");
                    }
                    catch { }
                    SetLocation(Server.ConfigJson["location"].ToString());
                    break;
                }
                else//从机模式
                {
                    break;
                }
            }
            Server.IsSetting = false;
        }

        private void DataUpdate(bool e)
        {
            SensorMessage[0].Data = Server.pMSensors.PM1_0;
            SensorMessage[1].Data = Server.pMSensors.PM2_5;
            SensorMessage[2].Data = Server.pMSensors.PM10;
            RaisePropertyChanged("SensorMessage");
        }

        public void SetLocation(string str)
        {
            if (str != "" && str != null)
            {
                var desktopWorkingArea = System.Windows.SystemParameters.WorkArea;
                if (str.CompareTo("LeftTop") == 0)
                {
                    MainWindow.Left = 20;
                    MainWindow.Top = 20;
                }
                else if (str.CompareTo("LeftBottom") == 0)
                {
                    MainWindow.Left = 20;
                    MainWindow.Top = desktopWorkingArea.Bottom - MainWindow.Height - 20;
                }
                else if (str.CompareTo("RightTop") == 0)
                {
                    MainWindow.Left = desktopWorkingArea.Right - MainWindow.Width - 20;
                    MainWindow.Top = 20;
                }
                else if (str.CompareTo("RightBottom") == 0)
                {
                    MainWindow.Left = desktopWorkingArea.Right - MainWindow.Width - 20;
                    MainWindow.Top = desktopWorkingArea.Bottom - MainWindow.Height - 20;
                }
                else
                {
                    try
                    {
                        MainWindow.Left = Convert.ToInt32(Server.ConfigJson["x"]);
                        MainWindow.Top = Convert.ToInt32(Server.ConfigJson["y"]);
                    }
                    catch { }
                }
            }
            else
            {

            }
        }
    }

    public class SensorMessage : ViewModelBase
    {
        private string _Name;
        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
                RaisePropertyChanged("Name");
            }
        }

        private double _Data;
        public double Data
        {
            get
            {
                return _Data;
            }
            set
            {
                _Data = value;
                RaisePropertyChanged("Data");
            }
        }

        private string _Unit;
        public string Unit
        {
            get
            {
                return _Unit;
            }
            set
            {
                _Unit = value;
                RaisePropertyChanged("Unit");
            }
        }

        private Brush _Foregroud;
        public Brush Foregroud
        {
            get
            {
                return _Foregroud;
            }
            set
            {
                _Foregroud = value;
                RaisePropertyChanged("Foregroud");
            }
        }

        public string Tooltip
        {
            get;
            set;
        }
    }
}
